#!/bin/bash

#Created by turk 2015-10-29
#Domain 
#Version 1.0 



#if [ $# -lt 3 ]; then
	#	echo `date +"%Y-%m-%d %H:%M:%S"`  "Usage:<TABLENAME> <STARTROW> <ENDROW>"
	#	exit 1
#fi

TABLENAME=$1
STARTROW=$2
ENDROW=$3

WORKPATH=/home/turk
LOGPATH=${WORKPATH}/log
LOGFILE=${LOGPATH}/leak_import.log


DATAPATH=/home/leak
CONTROLPATH=${WORKPATH}/control



echo `date +"%Y-%m-%d %H:%M:%S"`  "------------------------------------" | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "---Shell Version 1.0 (2015-10-29)---"     | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "---        Leak data import      ---"  | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "------------------------------------"  | tee -a $LOGFILE
echo `date +"%Y-%m-%d %H:%M:%S"`  "Start..."  | tee -a $LOGFILE

#ORACLE TNS
TNS_NAME=BSMP
USERNAME=sds
PASSWORD=h16aug8v3w

function sqlldrfunc()
{
#SQLLOAD CMD
#siteInfo
CTLFILE=$1
DATAFILE=$2
FILENAME=${DATAFILE##*/}
echo "sqlldr ${USERNAME}/${PASSWORD}@${TNS_NAME} control=${CONTROLPATH}/${CTLFILE} data=${DATAFILE} log=${LOGPATH}/${CTLFILE}.log bad=${LOGPATH}/${CTLFILE}.bad" | tee -a $LOGFILE 
sqlldr ${USERNAME}/${PASSWORD}@${TNS_NAME} control=${CONTROLPATH}/${CTLFILE} data=${DATAFILE} log=${LOGPATH}/${FILENAME}.log bad=${LOGPATH}/${FILENAME}.bad
}




FILENUM=`ls ${DATAPATH}/siteInfo_*.txt | wc -l`
if [ $FILENUM -ge 1 ]; then
	for num in $( seq 1 $FILENUM )
	do
		SITEINFOFILE=`ls ${DATAPATH}/siteInfo_*.txt | sed -n "$num"p`
		echo `date +"%Y-%m-%d %H:%M:%S"`       "SITEINFOFILE:${SITEINFOFILE}" | tee -a $LOGFILE
		sqlldrfunc siteInfo.ctl ${SITEINFOFILE}
	done
fi

FILENUM=`ls ${DATAPATH}/serverInfo_*.txt | wc -l`
if [ $FILENUM -ge 1 ]; then
	for num in $( seq 1 $FILENUM )
	do
		SITEINFOFILE=`ls ${DATAPATH}/serverInfo_*.txt | sed -n "$num"p`
		echo `date +"%Y-%m-%d %H:%M:%S"`       "SERVERINFOFILE:${SITEINFOFILE}" | tee -a $LOGFILE
		sqlldrfunc serverInfo.ctl ${SITEINFOFILE}
	done
fi

FILENUM=`ls ${DATAPATH}/serverRisk_*.txt | wc -l`
if [ $FILENUM -ge 1 ]; then
	for num in $( seq 1 $FILENUM )
	do
		SITEINFOFILE=`ls ${DATAPATH}/serverRisk_*.txt | sed -n "$num"p`
		echo `date +"%Y-%m-%d %H:%M:%S"`       "SERVERRISKFILE:${SITEINFOFILE}" | tee -a $LOGFILE
		sqlldrfunc serverRisk.ctl ${SITEINFOFILE}
	done
fi


exit 0
